// 新风系统设备详情
//  ZYDeviceVSItemView.m
//  YiLin
//
//  Created by Zyark on 2018/6/4.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYDeviceVSItemView.h"
#import "ZYDevice.h"
#import "ZYInDoorView.h"
#import "ZYOutDoorView.h"
#import "ZYImageUtils.h"
#import "ZYModeSelectView.h"

@interface ZYDeviceVSItemView()<ZYModeSelectViewDelegate>
@property(nonatomic,strong) UIView *leftView;

@property(nonatomic,strong) UIView *middleView;

@property(nonatomic,strong) UIView *rightView;

@property(nonatomic,strong) UILabel *nameLabel;

/**
 模式图片
 */
@property(nonatomic,strong) UIImageView *modeImageView;

/**
 模式
 */
@property(nonatomic,strong) UILabel *modeLabel;

/**模式背景*/
@property(nonatomic,strong) UIView *modeBgView;


/**
 wifi
 */
@property(nonatomic,strong) UIImageView *wifiImageView;


/**
 室内
 */
@property(nonatomic,strong) UIButton *indoorBtn;


/**
 室外
 */
@property(nonatomic,strong) UIButton *outdoorBtn;

/**
 pm2.5背景
 */
@property(nonatomic,strong) UIView *pmBgView;

/**
 pm2.5Label
 */
@property(nonatomic,strong) UILabel *pmLabel;


/**
 Pm2.5质量Label
 */
@property(nonatomic,strong) UILabel *pmQualityLabel;


/**
 pm2.5指数label
 */
@property(nonatomic,strong) UILabel *pmExponentLabel;

@property(nonatomic,strong) ZYInDoorView *inDoorView;

@property(nonatomic,strong) ZYOutDoorView *outDoorView;


/**
 模式选择
 */
@property(nonatomic,strong) ZYModeSelectView *modeSelectView;

/**
 是否弹出
 */
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYDeviceVSItemView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
//    _leftView = [self setupSidesView];
//    _leftView.centerX = 0;
//    _leftView.top = 0;
//
//    _rightView = [self setupSidesView];
//    _rightView.centerX = self.right;
//    _rightView.top = 0;
    
    CGFloat width = ZYSCREEN_W;
    _middleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, self.height)];
    [_middleView setBackgroundColor:[UIColor clearColor]];
    _middleView.layer.cornerRadius = 5;
    _middleView.layer.masksToBounds = YES;
    [self addSubview:_middleView];
    _middleView.centerY = self.centerY;
    _middleView.centerX = self.centerX;
    
    /**wifi图片*/
    _wifiImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_home_main_wifi"]];
    [_middleView addSubview:_wifiImageView];
    _wifiImageView.left = 15;
    _wifiImageView.top = 10;
    
    
    /**设备名称*/
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.size = CGSizeMake(30, 15);
    _nameLabel.textColor = kDeviceLabelColor;
    _nameLabel.font = [UIFont systemFontOfSize:16];
    [_middleView addSubview:_nameLabel];
    _nameLabel.centerY = _wifiImageView.centerY;
    _nameLabel.left = _wifiImageView.right + 3;
    
    if(ZYiPhone4_OR_4s || ZYiPhone5_OR_5c_OR_5s){
        _modeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
    }else{
        _modeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0.2 * self.width, 0.2 * self.width)];
    }
    _modeBgView.backgroundColor = [UIColor whiteColor];
    _modeBgView.layer.cornerRadius = 10;
    _modeBgView.layer.masksToBounds = YES;
    [_middleView addSubview:_modeBgView];
    _modeBgView.right = _middleView.width - 15;
    _modeBgView.top = 10;
    
    /**模式图片*/
    _modeImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_home_mode_custom_selected"]];
    [_modeBgView addSubview:_modeImageView];
    _modeImageView.centerX = _modeBgView.width / 2;
    _modeImageView.top = 5;
    [_modeImageView setUserInteractionEnabled:YES];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSlectMode)];
    [_modeBgView addGestureRecognizer:tap];
    
    /**模式*/
    _modeLabel = [[UILabel alloc] init];
    _modeLabel.size = CGSizeMake(60, 15);
    _modeLabel.textColor = kMainColor;
    _modeLabel.textAlignment = NSTextAlignmentCenter;
    _modeLabel.font = [UIFont systemFontOfSize:14];
    [_modeBgView addSubview:_modeLabel];
    _modeLabel.centerX = _modeImageView.centerX;
    _modeLabel.top = _modeImageView.bottom + 5;
    
    //室内 室外按钮
    _indoorBtn = [self setupButton];
    _outdoorBtn = [self setupButton];
    _indoorBtn.tag = ZYRoomTypeIn;
    _outdoorBtn.tag = ZYRoomTypeOut;
    _indoorBtn.left = _nameLabel.left;
    _indoorBtn.top = _nameLabel.bottom + 10;
    
    _outdoorBtn.left = _indoorBtn.right + 5;
    _outdoorBtn.centerY = _indoorBtn.centerY;
    
    //pm2.5背景
    _pmBgView = [[UIView alloc] initWithFrame:CGRectMake(7, 0, _middleView.width - 30, 0.38 * _middleView.height)];
    _pmBgView.backgroundColor = kPmBGColor;
    _pmBgView.layer.cornerRadius = 20;
    _pmBgView.layer.masksToBounds = YES;
    [_middleView addSubview:_pmBgView];
    _pmBgView.top = _modeBgView.bottom + 10;
    
    //pm2.5指数
    _pmExponentLabel = [[UILabel alloc] init];
    _pmExponentLabel.textColor = kDeviceLabelColor;
    _pmExponentLabel.font = [UIFont systemFontOfSize:74];
    _pmExponentLabel.textAlignment = NSTextAlignmentCenter;
    _pmExponentLabel.size = CGSizeMake(_pmBgView.width / 2 , _pmBgView.height * 0.8);
    [_middleView addSubview:_pmExponentLabel];
    _pmExponentLabel.centerY = _pmBgView.centerY;
    _pmExponentLabel.left = _pmBgView.left + 25;
    
    //pm2.5Label
    _pmLabel = [[UILabel alloc] init];
    _pmLabel.textColor = kDeviceLabelColor;
    _pmLabel.font = [UIFont systemFontOfSize:18];
    _pmLabel.width = 55;
    _pmLabel.height = 20;
    [_middleView addSubview:_pmLabel];
    _pmLabel.left = _pmExponentLabel.right;
    _pmLabel.top = _pmExponentLabel.top;
    
    //pm2.5质量label
    _pmQualityLabel = [[UILabel alloc] init];
    _pmQualityLabel.textColor = kDeviceLabelColor;
    _pmQualityLabel.font = [UIFont systemFontOfSize:18];
    _pmQualityLabel.textAlignment = NSTextAlignmentRight;
    _pmQualityLabel.width = (_pmBgView.right - _pmLabel.right) - 15;
    _pmQualityLabel.height = 18;
    [_middleView addSubview:_pmQualityLabel];
    _pmQualityLabel.right = _pmLabel.right;
    _pmQualityLabel.bottom = _pmExponentLabel.bottom - _pmBgView.height * 0.2;
    
    _inDoorView = [[ZYInDoorView alloc] initWithFrame:CGRectMake(0, 0, _middleView.width, _middleView.height - _pmBgView.bottom)];
    _outDoorView = [[ZYOutDoorView alloc] initWithFrame:CGRectMake(0, 0, _middleView.width, _middleView.height - _pmBgView.bottom)];
    [_middleView addSubview:_inDoorView];
    [_middleView addSubview:_outDoorView];
    _inDoorView.top = _pmBgView.bottom;
    _inDoorView.centerX = _middleView.width / 2;
    _outDoorView.centerY = _inDoorView.centerY;
    _outDoorView.centerX = _middleView.width / 2;

    _modeSelectView = [[ZYModeSelectView alloc] initWithFrame:CGRectMake(0, 0, _middleView.width * 0.9, 0) deviceType:ZYDeviceTypeVentilationSystem];
    [self addSubview:_modeSelectView];
    _modeSelectView.centerX = _middleView.centerX;
    _modeSelectView.top = _modeLabel.bottom;
    _modeSelectView.delegate = self;
    
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    
    _isAlterSelectView = NO;
//    CGFloat realHeight = _modeSelectView.top + _modeSelectView.realHeight;
//    if(realHeight > self.height){
//        self.height = realHeight;
//    }
    //默认室内
    [self switchRoomType:_indoorBtn];
}

-(UILabel *)setupLabel{
    UILabel *label = [[UILabel alloc] init];
    label.width = _middleView.width / 2 - 30;
    label.height = 15;
    label.font = [UIFont systemFontOfSize:14];
    label.textAlignment = NSTextAlignmentLeft;
    label.textColor = kDeviceLabelColor;
    [_middleView addSubview:label];
    return label;
}

-(UIButton *)setupButton{
    UIButton *button = [[UIButton alloc] init];
    button.size = CGSizeMake(56, 21);
    [button setTitleColor:kMainColor forState:UIControlStateSelected];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:[UIColor whiteColor] rect:button.bounds] forState:UIControlStateSelected];
    [button setBackgroundImage:[ZYImageUtils imageWithColor:kMainColor rect:button.bounds] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(switchRoomType:) forControlEvents:UIControlEventTouchUpInside];
    [button.titleLabel setFont:[UIFont systemFontOfSize:13]];
    button.layer.cornerRadius = 7;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor whiteColor].CGColor;
    [_middleView addSubview:button];
    return button;
}

-(UIView *)setupSidesView{
    UIView *view = [[UIView alloc] init];
    view.size = CGSizeMake(50, self.height);
    [view setBackgroundColor:RGBA(255, 255, 255, 0.7)];
    view.layer.cornerRadius = 5;
    view.layer.masksToBounds = YES;
    [self addSubview:view];
    return view;
}

#pragma mark - Event
-(void)doSlectMode{
    [_modeSelectView showOrHidden];
//    if(_isAlterSelectView){
//
//    }else{
//        [UIView animateWithDuration:0.3 animations:^{
//
//        }];
//    }
//    if([super.delegate respondsToSelector:@selector(deviceItemView:setModeTypeWithdevice:)]){
//        [super.delegate deviceItemView:self setModeTypeWithdevice:super.device];
//    }
}

-(void)switchRoomType:(UIButton *)button{
    _indoorBtn.selected = NO;
    _outdoorBtn.selected = NO;
    button.selected = YES;
    BOOL isIn = (button.tag == ZYRoomTypeIn);
    [UIView animateWithDuration:0.3 animations:^{
        _inDoorView.hidden = !isIn;
        _outDoorView.hidden = isIn;
        _modeBgView.hidden = !isIn;
//        _modeImageView.hidden = !isIn;
//        _modeLabel.hidden = !isIn;
    }];
    if(isIn){
        _pmExponentLabel.text = super.device.pmExponent;
        _pmQualityLabel.text = super.device.pmQuality;
    }else{
        _pmExponentLabel.text = super.device.outPmExponent;
        _pmQualityLabel.text = super.device.outPmQuality;
    }
}

-(void)setDevice:(ZYDevice *)device{
    super.device = device;
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    _nameLabel.text = device.name;
    NSDictionary *attributes = @{NSFontAttributeName:_nameLabel.font,};
    CGSize textSize = [device.name boundingRectWithSize:CGSizeMake(_middleView.width * 3 / 4 , _nameLabel.height)
                                                options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    _nameLabel.width = textSize.width;
//    _wifiImageView.left = _nameLabel.right + 3;
    _inDoorView.device = device;
    _outDoorView.device = device;
    _pmExponentLabel.text = device.pmExponent;
    _pmLabel.text = @"PM2.5";
    _pmQualityLabel.text = device.pmQuality;
    if(device.isConnect){
        _wifiImageView.image = [UIImage imageNamed:@"ic_home_main_wifi"];
    }else{
        _wifiImageView.image = [UIImage imageNamed:@"ic_no_wifi"];
    }
    [_modeSelectView setupTitle];
    [_indoorBtn setTitle:[language getStringByKey:LC_HOME_MAIN_ROOM_TYPE_IN] forState:UIControlStateNormal];
    [_indoorBtn setTitle:[language getStringByKey:LC_HOME_MAIN_ROOM_TYPE_IN] forState:UIControlStateSelected];
    
    [_outdoorBtn setTitle:[language getStringByKey:LC_HOME_MAIN_ROOM_TYPE_OUT] forState:UIControlStateNormal];
    [_outdoorBtn setTitle:[language getStringByKey:LC_HOME_MAIN_ROOM_TYPE_OUT] forState:UIControlStateSelected];
    [UIView animateWithDuration:0.3 animations:^{
        [self layoutIfNeeded];
    }];
    [self setModeType:device.vsModeType];
    
    //判断是否锁定设备
    _modeSelectView.userInteractionEnabled = (!super.device.isLock && super.device.isOpen);
}

-(void)setIsLock:(BOOL)isLock{
    super.device.isLock = isLock;
    //判断是否锁定设备
    _modeSelectView.userInteractionEnabled = (!isLock && super.device.isOpen);
}

-(void)setIsOpen:(BOOL)isOpen{
    super.device.isOpen = isOpen;
    //判断是否锁定设备
    _modeSelectView.userInteractionEnabled = (isOpen && !super.device.isLock);
}

-(void)setModeType:(ZYVSModeType)modeType{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    switch (modeType) {
        case ZYVSModeTypeCommon:
            _modeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_common_selected"];
            _modeLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_COMMON];
            break;
        case ZYVSModeTypeSleeping:
            _modeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_sleeping_selected"];
            _modeLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_SLEEPING];
            break;
        case ZYVSModeTypeMixedWind:
            _modeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_mixed_wind_selected"];
            _modeLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_MIXED_WIND];
            break;
        case ZYVSModeTypePurification:
            _modeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_purification_selected"];
            _modeLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_PURIFICATION];
            break;
        case ZYVSModeTypeAuto:
            _modeImageView.image = [UIImage imageNamed:@"ic_vs_mode_type_auto_selected"];
            _modeLabel.text = [language getStringByKey:LC_VS_MODE_TYPE_AUTO];
            break;
        case ZYVSModeTypManual:
            _modeImageView.image = [UIImage imageNamed:@"ic_mode_type_manual_selected"];
            _modeLabel.text = [language getStringByKey:LC_DEVICE_MODE_TYPE_MANUAL];
        default:
            break;
    }
}

#pragma mark ZYModeSelectViewDelegate
-(void)modeSelectView:(ZYModeSelectView *)modeSelectView setModeType:(NSUInteger)modeType{
    if(super.device.isLock){
        return;
    }
    if(super.device.modeType == modeType){
        return;
    }
    super.device.modeType = modeType;
    [self setModeType:modeType];
    if([super.delegate respondsToSelector:@selector(deviceItemView:setModeType:device:)]){
        [super.delegate deviceItemView:self setModeType:modeType device:super.device];
    }
}

#pragma mark - public
-(void)setScrollStatus:(BOOL)isScroll{
//    [UIView animateWithDuration:0.3 animations:^{
//        [_leftView setHidden:isScroll];
//        [_rightView setHidden:isScroll];
//    }];
    [_modeImageView setUserInteractionEnabled:!isScroll];
}

@end
